﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ClassDAL;// 数据访问层
using ClassModel;
using ClassCommon;

namespace ClassBLL
{
    public class t_classDemo_UserInfo_BLL
    {

        /// <summary>
        /// 插入用户注册数据
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public static int InsertAccount(t_classDemo_UserInfo userInfo)
        {
            if (userInfo == null) return -1;

            // 对用户的密码进行MD5编码
            userInfo.userPassword = EncryptHelper.MD5Encrypt(userInfo.userPassword);

            return t_classDemo_UserInfo_DAL.InsertAccount(userInfo);
        }


        public static t_classDemo_UserInfo GetUserInfoById(int id)
        {
            return t_classDemo_UserInfo_DAL.GetUserInfoById(id);

        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>返回用户id --- 不等于 0 就表示登录成功</returns>
        public static int Login(string userName, string password)
        {
            password = EncryptHelper.MD5Encrypt(password);
            int userId = t_classDemo_UserInfo_DAL.Login(userName, password);
            if (userId > 0)
            {
                string encodeCookie = EncryptHelper.Encrypt(userId.ToString());
                HttpCookieHelper.SetCookie(ConfigSetting.Cookie_UserID, encodeCookie);
            }

            return userId;
        }

          /// <summary>
        /// 删除指定账户信息
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public static int DeleteWithAccount(string account)
        {
            return t_classDemo_UserInfo_DAL.DeleteWithAccount(account);
        }

         /// <summary>
        /// 用户找回密码
        /// </summary>
        /// <param name="newPassword">新密码</param>
        /// <param name="userAccount">修改的账户</param>
        /// <returns>false 失败 true 成功</returns>
        public static bool UpdatePassword(string newPassword, string userAccount)
        {
            if (string.IsNullOrEmpty(newPassword) || string.IsNullOrEmpty(userAccount))
            {
                throw new ArgumentNullException("newPassword | userAccount is Null");
            }

            string newPasswordEncode = EncryptHelper.MD5Encrypt(newPassword);

            return t_classDemo_UserInfo_DAL.UpdatePassword(newPasswordEncode, userAccount);
        }

        /// <summary>
        /// 检查当前账号是否存在
        /// </summary>
        /// <param name="userAccount"></param>
        /// <returns></returns>
        public static bool checkUserExists(string userAccount)
        {
            userAccount = userAccount.Trim();
            return t_classDemo_UserInfo_DAL.checkUserExists(userAccount);
        }
        
    }
}
